package com.example.myblog1_0.controller;

import com.example.myblog1_0.model.MyUser;
import com.example.myblog1_0.service.UserService;
import com.example.myblog1_0.util.FileUtil;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.security.Principal;

/**
 * @Author: xc
 * @Date: 2018/12/23 17:13
 * @Description:
 **/
@RestController
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("/getUserInfoByUsername")
    public JSONObject getUserInfoByUsername(MyUser myUser){
        MyUser findUser = userService.findUserInfoByName(myUser.getUsername());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("nickname",findUser.getNickname());
        return jsonObject;
    }
    @GetMapping("/getUserInfo")
    public JSONObject getUserInfo(@AuthenticationPrincipal Principal principal){
        try {
            String username = principal.getName();
            return userService.getUserInfoByUsername(username);
        }catch (Exception e){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("status",0);
            jsonObject.put("message","暂无用户登陆");
            return  jsonObject;
        }



    }
    @PostMapping("/updateNickname")
    public JSONObject updateNickname(@RequestParam("nickname") String nickname,
                                        @AuthenticationPrincipal Principal principal){

        String username = principal.getName();
        return userService.updateNickname(username,nickname);
    }
    @PostMapping("/uploadAvatar")
    public JSONObject updateAvatar(HttpServletRequest request,
                                   @AuthenticationPrincipal Principal principal){
        String username = principal.getName();
        JSONObject jsonObject = new JSONObject();
        String imgInfo = request.getParameter("imgInfo");

        //获得上传文件的后缀名
        int index = imgInfo.indexOf(";base64,");
        String strFileExtendName = "." + imgInfo.substring(11,index);
        //将base64字符前面的图片信息data:image/jpeg;base64,去除掉。
        imgInfo = imgInfo.substring(index + 8);

        //上传文件
        try {
            FileUtil fileUtil = new FileUtil();
            String filePath = this.getClass().getResource("/").getPath().substring(1) + "userImg";
            File file = fileUtil.base64ToFile(filePath,imgInfo,username + strFileExtendName);
            String url = fileUtil.uploadFile(file,"user");
            jsonObject = userService.updateAvatar(username,url);
            return jsonObject;
        }catch (Exception e){
            jsonObject.put("status",403);
            jsonObject.put("message","图像上传失败");
            return jsonObject;
        }

    }


}
